package com.ruoyi.web.controller.system;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import com.ruoyi.common.core.domain.NameValue;
import com.ruoyi.common.core.domain.PieNameValue;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysPostService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Employee;
import com.ruoyi.system.service.IEmployeeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 报表控制类
 */
@Controller
@RequestMapping("/system/report")
public class ReportController extends BaseController{

    private String prefix = "system/report";

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private ISysPostService sysPostService;

    @RequiresPermissions("system:report:view")
    @GetMapping()
    public String employee()
    {
        return prefix + "/report";
    }

    @RequiresPermissions("system:report:countemployeebydept")
    @RequestMapping("/countemployeebydept")
    @ResponseBody
    public List<PieNameValue> countemployeebydept() {
        List<String> deptName = sysDeptService.selectAllDeptName();
        List<PieNameValue> pieNameValueList= new ArrayList<>();
        for(String name: deptName){
            PieNameValue pieNameValue = new PieNameValue();
            pieNameValue.setName(name);
            pieNameValue.setValue(employeeService.countEmployeeByDept(name));
            pieNameValueList.add(pieNameValue);
        }
        return pieNameValueList;
    }

    @RequiresPermissions("system:report:countemployeebypost")
    @RequestMapping("/countemployeebypost")
    @ResponseBody
    public List<PieNameValue> countemployeebypost() {
        List<String> postName = sysPostService.selectAllPostName();
        List<PieNameValue> pieNameValueList= new ArrayList<>();
        for(String name: postName){
            PieNameValue pieNameValue = new PieNameValue();
            pieNameValue.setName(name);
            pieNameValue.setValue(employeeService.countEmployeeByPost(name));
            pieNameValueList.add(pieNameValue);
        }
        return pieNameValueList;
    }

    @RequiresPermissions("system:report:avgsalarybydept")
    @RequestMapping("/avgsalarybydept")
    @ResponseBody
    public NameValue avgsalarybydept() {
        List<Double> valueList = new ArrayList<>();
        List<String> deptName = sysDeptService.selectAllDeptName();
        for(String name: deptName){
            valueList.add(employeeService.avgSalaryByDept(name));
        }
        NameValue nameValue = new NameValue();
        nameValue.setNames(deptName);
        nameValue.setValues(valueList);
        return nameValue;
    }

    @RequiresPermissions("system:report:avgsalarybypost")
    @RequestMapping("/avgsalarybypost")
    @ResponseBody
    public NameValue avgsalarybypost() {
        List<Double> valueList = new ArrayList<>();
        List<String> postName = sysPostService.selectAllPostName();
        for(String name: postName){
            valueList.add(employeeService.avgSalaryByPost(name));
        }
        NameValue nameValue = new NameValue();
        nameValue.setNames(postName);
        nameValue.setValues(valueList);
        return nameValue;
    }

    @RequiresPermissions("system:report:countattendancebydept")
    @RequestMapping("/countattendancebydept")
    @ResponseBody
    public NameValue countattendancebydept() {
        List<Double> valueList = new ArrayList<>();
        List<String> deptName = sysDeptService.selectAllDeptName();
        for(String name: deptName){
            if(employeeService.countEmployeeByDept(name) == 0){
                valueList.add(0.0);
            }
            else {
                valueList.add((employeeService.countAttendanceByDept(name)/ employeeService.countEmployeeByDept(name))*100);
            }        }
        NameValue nameValue = new NameValue();
        nameValue.setNames(deptName);
        nameValue.setValues(valueList);
        return nameValue;
    }

    @RequiresPermissions("system:report:countattendancebypost")
    @RequestMapping("/countattendancebypost")
    @ResponseBody
    public NameValue countattendancebypost() {
        List<Double> valueList = new ArrayList<>();
        List<String> postName = sysPostService.selectAllPostName();
        for(String name: postName){
            if(employeeService.countEmployeeByPost(name) == 0){
                valueList.add(0.0);
            }
            else {
                valueList.add((employeeService.countAttendanceByPost(name)/ employeeService.countEmployeeByPost(name))*100);
            }
        }
        NameValue nameValue = new NameValue();
        nameValue.setNames(postName);
        nameValue.setValues(valueList);
        return nameValue;
    }

    @RequiresPermissions("system:report:countlatebydept")
    @RequestMapping("/countlatebydept")
    @ResponseBody
    public NameValue countlatebydept() {
        List<Double> valueList = new ArrayList<>();
        List<String> deptName = sysDeptService.selectAllDeptName();
        for(String name: deptName){
            valueList.add(employeeService.countLateByDept(name));
        }
        NameValue nameValue = new NameValue();
        nameValue.setNames(deptName);
        nameValue.setValues(valueList);
        return nameValue;
    }

    @RequiresPermissions("system:report:countlatebypost")
    @RequestMapping("/countlatebypost")
    @ResponseBody
    public NameValue countlatebypost() {
        List<Double> valueList = new ArrayList<>();
        List<String> postName = sysPostService.selectAllPostName();
        for(String name: postName){
            valueList.add(employeeService.countLateByPost(name));
        }
        NameValue nameValue = new NameValue();
        nameValue.setNames(postName);
        nameValue.setValues(valueList);
        return nameValue;
    }

}
